import settings
import bsddb
import struct
import sys

class Database:
    def __init__(self):
        self.urlid = bsddb.btopen(settings.db_urlid,'c')
        self.idurl = bsddb.btopen(settings.db_idurl,'c')
        
        self.webgraph = open(settings.db_webgraph,'wb')
        self.urlid['totalurl'] = '0'
        self.totalurl = 0

    def addurl(self,url):
        if not self.urlid.has_key(url) :
            self.totalurl = id = self.totalurl + 1
            self.urlid['totalurl'] = str(id)
            self.urlid[str(url)] = str(id)
            self.urlid.sync()

            self.idurl[str(id)] = str(url)
            self.idurl.sync()

            return id
        else:
            return int(self.urlid[str(url)])

    def insertnode(self,src,dest):
        #print src,dest
        data = struct.pack('i',src)
        self.webgraph.write(data)

        data = struct.pack('i',len(dest))
        self.webgraph.write(data)

        for d in dest:
            data = struct.pack('i',d)
            self.webgraph.write(data)

    def printgraph(self):
        binfile = open(settings.db_webgraph,'rb')
        intsize = struct.calcsize('i')

        while 1:
            data = binfile.read(intsize)
            if data == '':
                break
            src = struct.unpack('i',data)
            print '>',src[0],

            data = binfile.read(intsize)
            if data == '':
                break
            outdegree = struct.unpack('i',data)
            print outdegree[0],
            
            for i in range(outdegree[0]):
                data = binfile.read(intsize)
                if data == '':
                    break
                dest = struct.unpack('i',data)
                print dest[0],
            print ''

        binfile.close()

    def savetitle(self,src,title):
        dbtitle = bsddb.btopen(settings.db_title,'c')
        dbtitle[str(src)] = str(title)

    def close(self):
        self.urlid.close()
        self.webgraph.close()
